package com.jiazhong.example.project.shoppingcart.service.impl;

import com.jiazhong.example.project.shoppingcart.bean.Books;
import com.jiazhong.example.project.shoppingcart.mapper.BooksMapper;
import com.jiazhong.example.project.shoppingcart.mapper.UsersMapper;
import com.jiazhong.example.project.shoppingcart.service.BooksService;
import com.jiazhong.example.project.shoppingcart.util.JsonResult;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class BooksServiceImpl implements BooksService {

    private BooksMapper booksMapper;
    private SqlSession session;
    private SqlSessionFactory ssf;

    public BooksServiceImpl() {
        InputStream inputStream = UsersServiceImpl.class.getClassLoader().getResourceAsStream("config.xml");
        ssf = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Override
    public JsonResult findAll() {
        session = ssf.openSession();
        booksMapper = session.getMapper(BooksMapper.class);
        List<Books> all = booksMapper.findAll();
        return new JsonResult(all);
    }
}
